# -*- coding: utf-8 -*-
# @Time    : 2022/10/28 14:46
# @Author  : ZSQ
# @Email   : zsq199170918@163.com
# @FileName: main_ftx.py
# @Software: PyCharm

import os
import time
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED

FLAG = False


def ftx_spider(seconds, city):
    if seconds:
        time.sleep(seconds)
        print(f'{"*" * 60}\n房天下二手房-{city}-爬取开始')
    os.system(f'scrapy crawl fangtianxia -a crawl={city}')
    global FLAG
    FLAG = True
    print(f'{"*" * 60}\n房天下二手房-{city}-爬取完成')
    return


def ftx_cookies_spider():
    print(f'{"*" * 60}\n房天下二手房cookies任务开始')
    while True:
        os.system('scrapy crawl fangtianxia_cookie')
        if FLAG:
            break
        else:
            time.sleep(5)
            os.system('taskkill /F /IM chrome.exe')
    print(f'{"*" * 60}\n房天下二手房cookies任务结束')
    return


def run():
    with ThreadPoolExecutor(max_workers=5) as task:  # 创建一个最大容纳数量为5的线程池
        # task1 = task.submit(ftx_spider, 10, '成都、深圳、昆明、海南、恩施')
        # task1 = task.submit(ftx_spider, 10, '成都')
        # task1 = task.submit(ftx_spider, 10, '深圳')
        # task1 = task.submit(ftx_spider, 10, '昆明')
        # task1 = task.submit(ftx_spider, 10, '三亚')
        # task1 = task.submit(ftx_spider, 10, '海南')
        task1 = task.submit(ftx_spider, 10, '淮南、六安、宿州、亳州')
        # task1 = task.submit(ftx_spider, 10, '铜陵、淮北、池州')
        # task1 = task.submit(ftx_spider, 10, '黄山、阜阳、滁州、蚌埠')
        # task1 = task.submit(ftx_spider, 10, '开封')
        # task1 = task.submit(ftx_spider, 10, '南宁')
        task2 = task.submit(ftx_cookies_spider, )
        wait([task1, task2], return_when=ALL_COMPLETED)
        print(f'{"*" * 60}\n房天天二手房爬虫任务全部结束')


if __name__ == '__main__':
    run()
